﻿// -----------------------------------------------------------------------
// <copyright file="Zoom.cs" company="FCIS">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ImagePackage.Core.GeometryOperations
{
    using System;
    using Model;
    using Utilities;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Zoom : IOperation
    {
        /// <summary>
        /// The input image to act upon
        /// </summary>
        protected readonly Image InputImage;

        /// <summary>
        /// Initializes a new instance of the <see cref="Zoom"/> class.
        /// </summary>
        /// <param name="inputImage">The input image.</param>
        public Zoom(Image inputImage)
        {
            this.InputImage = inputImage;
        }

        /// <summary>
        /// Gets or sets the zoom factor.
        /// </summary>
        /// <value>
        /// The zoom factor.
        /// </value>
        public double ZoomFactor { get; set; }

        /// <summary>
        /// Applies the operation.
        /// </summary>
        /// <returns>
        /// Image object
        /// </returns>
        public Image Apply()
        {
            Logger.Debug("Start applying Zoom Operation");
            try
            {
                int width = (int)(this.InputImage.Width * this.ZoomFactor / 100);
                int height = (int)(this.InputImage.Height * this.ZoomFactor / 100);
                Image buffer = new Image(width, height);
                for (int i = 0; i < width; i++)
                {
                    for (int j = 0; j < height; j++)
                    {
                        Pixel newPixel = this.InputImage[(int)Math.Floor(i * 100 / this.ZoomFactor), (int)Math.Floor(j * 100 / this.ZoomFactor)];

                        buffer[i, j] = newPixel;
                    }
                }

                return buffer;
            }
            catch (Exception ex)
            {
                Logger.LogException("Something wrong happened in Zoom Operation", ex);
                throw;
            }
            finally
            {
                Logger.Debug("Finished applying Zoom Operation");
            }
        }
    }
}
